package jp.gr.java_conf.ryz.summarize.android.activity;

import java.util.ArrayList;
import java.util.List;

import jp.gr.java_conf.ryz.summarize.android.R;
import jp.gr.java_conf.ryz.summarize.android.logic.DataCreator;
import jp.gr.java_conf.ryz.summarize.android.model.Post;
import jp.gr.java_conf.ryz.summarize.android.model.Thred;
import jp.gr.java_conf.ryz.summarize.android.view.PostPanel;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.CheckBox;

public class ViewPostActivity extends Activity
{
	public static final String PARAM_THREAD = "param_thread";
	
	private Thred _thread;

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.activity_view_post, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.menu_generate_html:
			Intent intent = new Intent(ViewPostActivity.this, GenerateResultActivity.class);
			
			ArrayList<Post> posts = getSelectedPosts();
//			Post[] postArray = posts.toArray(new Post[posts.size()]);
			intent.putExtra(GenerateResultActivity.PARAM_POSTS, posts);
			
			startActivity(intent);
			break;
		case R.id.menu_SelectAllPost:
			changeSelectAllPost(true);
			break;
		case R.id.menu_UnselectAllPost:
			changeSelectAllPost(false);
			break;
		case R.id.menu_Settings:
			break;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_post);
		
		Bundle extras = getIntent().getExtras();
		_thread = (Thred)extras.getSerializable(PARAM_THREAD);
		
		init();
	}
	
	
	private void init()
	{
		final ViewGroup postContainerView = (ViewGroup)findViewById(R.id.activity_view_post_post_container);
		
		new AsyncTask<Thred, Integer, List<Post>>()
		{

			@Override
			protected List<Post> doInBackground(Thred... params)
			{
				List<Post> posts = DataCreator.getPosts(params[0], true);
				return posts;
			}

			@Override
			protected void onPostExecute(List<Post> result)
			{
				postContainerView.removeAllViews();
				for (Post each : result)
				{
					PostPanel postPanel = new PostPanel(ViewPostActivity.this);
					postPanel.setPost(each);
					postContainerView.addView(postPanel);
				}
			}
			
		}.execute(_thread);
	}
	
	private void changeSelectAllPost(boolean isSelect)
	{
		ViewGroup postContainerView = (ViewGroup)findViewById(R.id.activity_view_post_post_container);
		
		int childCount = postContainerView.getChildCount();
		for (int index = 0; index < childCount; index++)
		{
			PostPanel postPanel = (PostPanel)postContainerView.getChildAt(index);
			CheckBox selectCheck = (CheckBox)postPanel.findViewById(R.id.post_panel_selectCheck);
			selectCheck.setChecked(isSelect);
		}
	}

	private ArrayList<Post> getSelectedPosts()
	{
		ArrayList<Post> result = new ArrayList<Post>();
		ViewGroup postContainerView = (ViewGroup)findViewById(R.id.activity_view_post_post_container);
		
		int childCount = postContainerView.getChildCount();
		for (int index = 0; index < childCount; index++)
		{
			PostPanel postPanel = (PostPanel)postContainerView.getChildAt(index);
			CheckBox selectCheck = (CheckBox)postPanel.findViewById(R.id.post_panel_selectCheck);
			if (selectCheck.isChecked())
			{
				result.add(postPanel.getPost());
			}
		}
		
		return result;
	}
}
